/*
 * @file     g_powerkey.c
 * @brief    source file for setting powerkey.
 *
 * Change Logs:
 * Date           Author             Version        Notes
 * 2021-05-25     xubo               V1.0.0         the first version
 */

#include "g_powerkey.h"

/* Exported functions -------------------------------------------------------*/
/**
 * @method PWK_IsReady
 * @brief  Check whether bpk is ready or not
 * @param  None
 * @retval The status of powerkey ready flag
 */
static G_FlagStatus PWK_IsReady(void)
{
    if (ENABLE == MSYSCTRL->LPM_BUSY_CFG.bit.LPM_STATE)
    {
        return SET;
    }
    return RESET;
}

/**
 * @method PWK_Init
 * @brief  Power key parameter setting.
 * @param  off_time power off time
 *         This parameter can be:
 *         PWK_OFF_TIME_4S
 *         PWK_OFF_TIME_5S
 *         PWK_OFF_TIME_6S
 *         PWK_OFF_TIME_7S
 * @param  on_time power on time
 *         This parameter can be:
 *         PWK_ON_TIME_120MS
 *         PWK_ON_TIME_400MS
 *         PWK_ON_TIME_500MS
 *         PWK_ON_TIME_1000MS
 * @param  trigger_type power key interrupt trigger type
 *         This parameter can be: ENABLE or DISABLE.
 * @retval G_ErrorStatus
 */
G_ErrorStatus PWK_Init(uint16_t off_time, uint16_t on_time, PWK_TYPE trigger_type)
{

    _ASSERT(IS_PWK_OFF_TIME(off_time));
    _ASSERT(IS_PWK_ON_TIME(on_time));

    if (SET == PWK_IsReady())
    {
        return ERROR;
    }

    G_LPM_AccessCode(ENABLE);
    G_LPM_RegisterClk(ENABLE);
    /*power off time is (4+power_off_time) seconds,  min 4s, max 7s*/
    MLPM->PWK_CTRL.bit.OFF_TIME = off_time;
    /*power on time is 500ms*(power_on_time), min 500ms, max 2s*/
    MLPM->PWK_CTRL.bit.ON_TIME = on_time;
    /*power key interrupt trigger type*/
    MSYSCTRL->CHGR_EVENT_ICTRL.bit.PWK_IT = trigger_type;

    return SUCCESS;
}

/**
 * @method PWK_ITEnable
 * @brief  power key interrupt enable
 * @param  NewState: new state of the power key interrupt.
 *         This parameter can be: ENABLE or DISABLE.
 * @retval None
 */
void PWK_ITEnable(G_FunctionalState NewState)
{
    _ASSERT(IS_FUNCTIONAL_STATE(NewState));
    MSYSCTRL->CHGR_EVENT_ICTRL.bit.PWK_IE = NewState;
    MSYSCTRL->CHGR_EVENT_IRQ.bit.IE = NewState;
}

/**
 * @method PWK_InterruptType
 * @brief  PWK interrupt type select
 * @param  type: powerkey interrupt type
 *         This parameter can be: HIGH or LOW.
 * @retval None
 */
void PWK_InterruptType(PWK_TYPE newtype)
{
    _ASSERT(IS_INT_SELECT(newtype));
    MSYSCTRL->CHGR_EVENT_ICTRL.bit.PWK_IT = newtype;
}

/**
 * @method PWK_ClearITPendingBit
 * @brief  Clear power key interrupt flag
 * @param  None
 * @retval None
 */
void PWK_ClearITPendingBit(void)
{
	MSYSCTRL->CHGR_EVENT_ICTRL.bit.PWK_IT = !(MSYSCTRL->CHGR_EVENT_ICTRL.bit.PWK_IT);
    MSYSCTRL->CHGR_EVENT_IRQ.bit.PWK = SET;
}

/**
 * @method PWK_GetPinState
 * @brief  get power key pin status
 * @param  None
 * @retval NewState  power key pin status
 */
G_FlagStatus PWK_GetPinState(void)
{
    if (MSYSCTRL->pwk_state.reg)
    {
        return SET;
    }
    return RESET;
}

/************************ (C) COPYRIGHT GKT *****END OF FILE****/
